package sjtu.ist.warehouse4ws.tool;

import com.opensymphony.xwork2.ActionSupport;
import java.io.IOException;  
import java.io.PrintWriter;  
  
import javax.servlet.ServletContext;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  
  
import org.apache.struts2.ServletActionContext; 

public class BaseAction extends ActionSupport {

	
	public String jsonString;  
	  
    public void outJsonString(String str) {  
        getResponse().setContentType("text/javascript;charset=UTF-8");  
        outString(str);  
    }  
  
    /* 
     * public void outJson(Object obj) { 
     * outJsonString(JSONObject.fromObject(obj).toString()); } 
     *  
     * public void outJsonArray(Object array) { 
     * outJsonArray(JSONArray.fromObject(array).toString()); } 
     */  
  
    public void outString(String str) {  
        try {  
            PrintWriter out = getResponse().getWriter();  
            out.write(str);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
  
    public void outXMLString(String xmlStr) {  
        getResponse().setContentType("application/xml;charset=UTF-8");  
        outString(xmlStr);  
    }  
  
    /** 
     * 获得request 
     *  
     * @return 
     */  
    public HttpServletRequest getRequest() {  
        return ServletActionContext.getRequest();  
    }  
  
    /** 
     * 获得response 
     *  
     * @return 
     */  
    public HttpServletResponse getResponse() {  
        return ServletActionContext.getResponse();  
    }  
  
    /** 
     * 获得session 
     *  
     * @return 
     */  
    public HttpSession getSession() {  
        return getRequest().getSession();  
    }  
  
    /** 
     * 获得servlet上下文 
     *  
     * @return 
     */  
    public ServletContext getServletContext() {  
        return ServletActionContext.getServletContext();  
    }  
  
    public String getRealyPath(String path) {  
        return getServletContext().getRealPath(path);  
    }  
}
